<template>
	<el-dialog
		:title="dialogTitle"
		v-model="cuVisible"
		width="1200px"
		top="0"
		:destroy-on-close="true"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		:show-close="false"
	>
		<el-table
			v-loading="setLoading"
			:data="setForm"
			style="margin-top: -25px"
		>
			<el-table-column label="启用" align="center" prop="title" width="70">
				<template #default="scope">
					<el-switch
						v-model="scope.row.is_open"
						inline-prompt
						active-text="是"
						inactive-text="否"
					/>
				</template>
			</el-table-column>
			<el-table-column label="规格" prop="title">
				<template #default="scope">
					<span v-if="setForm.length < 2 && scope.row.title === '默认'">{{ goodsName }}</span>
					<span v-else>{{ scope.row.title }}</span>
				</template>
			</el-table-column>
			<el-table-column label="销售价" prop="sell_price" align="right" width="120">
				<template #default="scope">
					{{ getMoneyByMinute(scope.row.sell_price) }}
				</template>
			</el-table-column>
			<el-table-column label="拼团价" align="left" prop="sell_price" width="150">
				<template #default="scope">
					<el-input-number
						v-model="scope.row.gagl_price"
						controls-position="right"
						size="small"
						:precision="2"
						:min="0"
					/>
				</template>
			</el-table-column>
			<el-table-column label="当前库存" prop="stock" width="120">
				<template #default="scope">
					{{ scope.row.stock }}
				</template>
			</el-table-column>

			<el-table-column align="center" label="拼团库存模式" prop="custom_stock" width="170">
				<template #default="scope">
					<el-radio-group v-model="scope.row.custom_stock">
						<el-radio :label="2">自定义</el-radio>
						<el-radio :label="1">系统</el-radio>
					</el-radio-group>
				</template>
			</el-table-column>

			<el-table-column label="拼团库存" prop="gagl_stock" width="150">
				<template #default="scope">
					<el-input-number
						v-model="scope.row.gagl_stock"
						controls-position="right"
						size="small"
						:disabled="scope.row.custom_stock === 1"
						:min="0"
						:max="scope.row.stock"
					/>
				</template>
			</el-table-column>
		</el-table>

		<template #footer>
			<div class="dialog-footer">
				<el-button @click="cuVisible = false" size="mini">取消</el-button>
				<el-button type="primary" @click="handleConfirm" size="mini">确定</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script>
export default {
	name: "addGoodsBoxSelectPop",
	data() {
		return{
			activityId: 0,
			goodsId: 0,
			cuVisible: false,
			setLoading: false,
			goodsName: '',
			setForm: [],
		}
	},
	emits: ['confirm'],
	computed: {
		dialogTitle() {
			return `设置《${this.goodsName}》拼团价格`
		}
	},
	methods: {
		// 关闭弹窗
		closeDialog() {
			this.cuVisible = false;
		},
		show(_activityId,goodsId,goodsName) {
			this.activityId = _activityId;
			this.goodsId = goodsId;
			this.goodsName = goodsName;
			this.cuVisible = true;
			this.getGoodsOptionList(goodsId);
		},
		async getGoodsOptionList(goodsId) {
			const methodName = "get_activity_option";
			console.log(methodName);
			let res = await this.$API.shop.goods[methodName]({
				activity_id: this.activityId,
				id: goodsId
			});
			if (res.code === 0) {
				this.setForm = res.data.map((item) => {
					item.is_open = item.gagl_id > 0;
					item.gagl_price = parseInt(item.gagl_price) / 100;
					item.custom_stock = isNaN(parseInt(item.custom_stock)) ? 1 : parseInt(item.custom_stock);
					return item;
				});
			}
		},
		getMoneyByMinute(_val) {
			return this.$TOOL.getMoneyByMinute(_val);
		},
		async handleConfirm() {
			let postData = this.setForm.filter((item) => {
				return item.is_open;
			});
			postData = postData.map((item) => {
				return {
					option_id: item.option_id,
					title: item.title,
					activity_id: this.activityId,
					goods_id: this.goodsId,
					gagl_id: item.gagl_id,
					gagl_price: parseInt(item.gagl_price * 100),
					gagl_stock: item.gagl_stock,
					custom_stock: item.custom_stock,
				};
			});
			console.log(postData)
			if (postData.length === 0) {
				this.$message.error("请至少启用一个规格");
				return;
			}
			let res = await this.$API.shop.goods.set_activity_option({
				activity_id: this.activityId,
				data: postData
			});
			if (res.code !== 0) {
				this.$message.error("设置失败," + res.message);
				return;
			}
			this.$message.success("设置成功");
			this.cuVisible = false;
			this.$emit('confirm',postData);
		}
	}
}
</script>

<style scoped>

</style>
